using System.Collections;
using System.Collections.Generic;
using System.Linq;
using BCW.Friends;
using I2.Loc;

public class CopsAndRobbersGameMode : TeamGameMode
{
	private bool enoughPlayers;

	public override bool IsAutoStart()
	{
		return true;
	}

	public override bool CanStartNow()
	{
		bool flag = false;
		if (PhotonNetwork.room != null && PhotonNetwork.room.customProperties.ContainsKey("timerStarted"))
		{
			flag = (bool)PhotonNetwork.room.customProperties["timerStarted"];
		}
		if (flag)
		{
			return true;
		}
		CopsRobbersSettings copsRobbersSettings = (CopsRobbersSettings)settings.GetSettings(TypeGame.CopsAndRobbers);
		bool flag2 = GameController.instance.listPlayers.Count((PlayerBehavior p) => p.localTeam == PunTeams.Team.blue && p.photonView.owner.GetValueByKey(PlayerPropsKeys.startPVP, false)) >= copsRobbersSettings.minRobbers;
		bool flag3 = GameController.instance.listPlayers.Count((PlayerBehavior p) => p.localTeam == PunTeams.Team.red && p.photonView.owner.GetValueByKey(PlayerPropsKeys.startPVP, false)) >= copsRobbersSettings.minCops;
		return flag2 && flag3;
	}

	public override void StartRound()
	{
		base.StartRound();
		PlayerBehavior.MyPlayer.FirstSpawn(2f);
		CoroutineRunner.Invoke(2.5f, delegate
		{
			GameInfoWindow.instance.HideInterface();
			MapWindow.instance.gameObject.SetActive(true);
			GameController.instance.showWindowGame();
			CopsAndRobbersController.instance.SetPlayerInventoryAccordingToTeam(false);
		});
		enoughPlayers = true;
	}

	public override void RestartRound()
	{
		base.RestartRound();
		if (BCWFlagManager.instance != null)
		{
			BCWFlagManager.instance.winerTeam = PunTeams.Team.none;
		}
		if (GameController.instance.playerScript != null)
		{
			GameController.instance.playerScript.Points = 0;
		}
		if (FriendsUIGame.instance != null)
		{
			FriendsUIGame.instance.CleanInvitePlayer();
		}
		CoroutineRunner.Instance.StartCoroutine(WaitEndTdm());
	}

	public override void EndRound()
	{
		enoughPlayers = false;
		CopsAndRobbersController.instance.EndGame();
	}

	private IEnumerator WaitEndTdm()
	{
		while (PVPManager.instance.gameEnded)
		{
			yield return null;
		}
		CopsAndRobbersController.instance.Restart();
		TDMController.instance.RestartTDM();
		GameInfoWindow.instance.ShowStartGameWindow();
	}

	public override string GameModeTitle()
	{
		if (!enoughPlayers)
		{
			CopsRobbersSettings copsRobbersSettings = (CopsRobbersSettings)settings.GetSettings(TypeGame.CopsAndRobbers);
			return string.Format(ScriptLocalization.Get("Game_Common/sm_CopsAndRobbers_playersToStart"), copsRobbersSettings.minCops + copsRobbersSettings.minRobbers);
		}
		return ScriptLocalization.Get("Game_Common/sm_CopsAndRobbers_matchWillBegin");
	}

	public override string EndGameModeTitle()
	{
		return ScriptLocalization.Get("Game_Common/cr_over");
	}

	public override TDMResult GetRoundResult()
	{
		if (CopsAndRobbersController.instance.GetCurrentStage() < 0)
		{
			return (TDMController.myTeam != PunTeams.Team.blue) ? TDMResult.Loser : TDMResult.Winner;
		}
		List<PlayerBehavior> list = GameController.instance.listPlayers.Where((PlayerBehavior x) => x.localTeam == PunTeams.Team.red).ToList();
		List<PlayerBehavior> list2 = GameController.instance.listPlayers.Where((PlayerBehavior x) => x.localTeam == PunTeams.Team.blue).ToList();
		if (list.Count == 0)
		{
			BCWDebug.LogError("LOG: All Cops leave the game.");
			return (TDMController.myTeam == PunTeams.Team.red) ? TDMResult.Loser : TDMResult.Winner;
		}
		if (list2.Count == 0)
		{
			BCWDebug.LogError("LOG: All Robbers leave the game.");
			return (TDMController.myTeam == PunTeams.Team.blue) ? TDMResult.Loser : TDMResult.Winner;
		}
		BCWDebug.LogError("LOG: Time out.");
		return (TDMController.myTeam == PunTeams.Team.blue) ? TDMResult.Loser : TDMResult.Winner;
	}
}
